/**
 * @Company 
 * @Title: LoginController.java 
 * @Package com.jbinfo.qingcaowan.app.controller 
 * @author lizhenwei
 * @date 2016-07-11 下午1:06:21 
 * @version V1.0   
 */
package com.jbinfo.qingcaowan.app.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jbinfo.qingcaowan.base.dto.BaseNoticeDTO;
import com.jbinfo.qingcaowan.base.dto.NoticeUserDTO;
import com.jbinfo.qingcaowan.base.service.BaseNoticeService;
import com.jbinfo.qingcaowan.common.JSONResult;
import com.jbinfo.qingcaowan.user.dto.UserDTO;
import com.jbinfo.qingcaowan.user.service.UserService;

/**
 * @ClassName: LoginController
 * @Description: 通知公告controller
 * @author John Zhao
 */
/**
 * @author Administrator
 *
 */
@Controller
public class NoticeController {

	private static final Logger LOG = LoggerFactory.getLogger(NoticeController.class);

	@Resource
	private BaseNoticeService baseNoticeService;
	
	@Resource
	private UserService userService;

	// 查询通知公告数量
	@RequestMapping(value = "/{client}/getNoticeCnt")
	public @ResponseBody
	Object getNoticeCnt(@PathVariable("client") String client, UserDTO userDTO, NoticeUserDTO noticeUserDTO) {
		LOG.info("客户端为 " + client + " noticeUserDTO：" + noticeUserDTO.toString());
		
//		LoginContext loginContext = LoginContextUtil.getLoginContext(session);
//		
//		UserDTO userDTO = loginContext.getLoginUser();
//		String orgId = userDTO.getUserOrg();
		
		// 根据userId 查询orgId
//		JSONResult userJson = (JSONResult) userService.getUserInfoById(userDTO);
//		UserDTO user = userJson.getResultBean();
//		String orgId = user.getUserOrg();
//		noticeDTO.setOrgId(orgId);
		noticeUserDTO.setUserid(userDTO.getUserId());
		
		JSONResult jsonResult = (JSONResult) baseNoticeService.getNoticeCnt(noticeUserDTO);
		
		return jsonResult;
	}
	
	// 查询通知公告列表
	@RequestMapping(value = "/{client}/getNoticeList")
	public @ResponseBody
	Object getNoticeList(@PathVariable("client") String client, UserDTO userDTO, NoticeUserDTO noticeUserDTO) {
		LOG.info("客户端为 " + client + " noticeUserDTO：" + noticeUserDTO.toString());
		
//		LoginContext loginContext = LoginContextUtil.getLoginContext(session);
//		
//		UserDTO userDTO = loginContext.getLoginUser();
//		String orgId = userDTO.getUserOrg();
		// 根据userId 查询orgId
//		JSONResult userJson = (JSONResult) userService.getUserInfoById(userDTO);
//		UserDTO user = userJson.getResultBean();
//		String orgId = user.getUserOrg();
		
		noticeUserDTO.setUserid(userDTO.getUserId());
		
		JSONResult jsonResult = (JSONResult) baseNoticeService.getNoticeList(noticeUserDTO);
		
		return jsonResult;
	}
	
	// 更新通知公告
	@RequestMapping(value = "/{client}/updateNotice")
	public @ResponseBody
	Object updateNotice(@PathVariable("client") String client, 
			HttpServletResponse response, UserDTO userDTO, BaseNoticeDTO baseNoticeDTO) {
		LOG.info("客户端为 " + client + " baseNoticeDTO：" + baseNoticeDTO.toString());
		
		//noticeDTO.setNoticeStatus("1");
		NoticeUserDTO noticeUserDTO = new NoticeUserDTO();
		noticeUserDTO.setNoticeid(baseNoticeDTO.getId());
		noticeUserDTO.setUserid(userDTO.getUserId());
		noticeUserDTO.setHadread("1");
		noticeUserDTO.setReadtime(new Date());
		JSONResult jsonResult = (JSONResult) baseNoticeService.updateNoticeStatus(noticeUserDTO);
		
		return jsonResult;
	}

}
